package com.ftl.activities;

import java.sql.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.ftl.framework.BitmapImg;
import com.ftl.saludos.*;

public class EventoMarcoActivity extends Activity {

    private Bundle mParams;
    protected String imagenFinal;
    public int marco = 0; 
    public Bitmap bmpMarco;

    private Gallery gallery;
    private ImageView imgView;
    private ImageButton btnSeguir;

    private Integer[] Imgid = {
            R.drawable.marco1, R.drawable.marco2, R.drawable.marco2  
    };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.evento_marco);

        imgView 	= (ImageView)findViewById(R.id.ivImgMarco);	
        gallery 	= (Gallery) findViewById(R.id.galMarcos);
        btnSeguir	= (ImageButton)findViewById(R.id.btnSeguir);
        
        
        mParams = getIntent().getExtras();
        
    	final Drawable d = Drawable.createFromPath(mParams.getString(EventosDBAdapter.KEY_RUTA_FOTO));
    	imgView.setBackgroundDrawable(d);        
        
        if (mParams.getLong(EventosDBAdapter.KEY_ID) > 0)
        {
        	//Recupero la info del evento y la cargo
        	String sRutaImg = mParams.getString(EventosDBAdapter.KEY_RUTA_FOTO);
        	long id = mParams.getLong(EventosDBAdapter.KEY_ID);
        	marco = mParams.getInt(EventosDBAdapter.KEY_MARCO);
        }
        else
        {
        	marco = Imgid[0];
        	
        }
        
        bmpMarco = BitmapFactory.decodeResource(getResources(), marco);
        imgView.setImageBitmap(bmpMarco);
        
		gallery.setAdapter(new AddImgAdp(EventoMarcoActivity.this));
		
		gallery.setOnItemClickListener(new OnItemClickListener() {
		   public void onItemClick(AdapterView parent, View v, int position, long id) {
		        
			   	marco = Imgid[position];
				bmpMarco = BitmapFactory.decodeResource(getResources(), marco);
				imgView.setImageBitmap(bmpMarco);
		        
		   }
		});
         
         btnSeguir.setOnClickListener(new OnClickListener() {
 			
 			private EventosDBAdapter dbaEventos;

			@Override
 			public void onClick(View v) {
 				
 				//Guardo los datos en base de datos.
 				//BitmapImg bmpFinal = new BitmapImg(((BitmapDrawable)d).getBitmap());
 				//bmpFinal.mergeBitmap(bmpMarco);
 				//imgView.setImageBitmap(bmpFinal.getBitmap());
 				
 				Evento e = new Evento();
 				e.setId(mParams.getLong(EventosDBAdapter.KEY_ID));
 				e.setTipo(new TipoEvento(mParams.getInt(EventosDBAdapter.KEY_ID_TIPO_EVENTO), "", ""));
 				e.setNombreReceptor(mParams.getString(EventosDBAdapter.KEY_NOMBRE_RECEPTOR));
 				e.setEmailReceptor(mParams.getString(EventosDBAdapter.KEY_EMAIL_RECEPTOR));
 				e.setFecha(new Date(mParams.getLong(EventosDBAdapter.KEY_FECHA)));
 				e.setComentario(mParams.getString(EventosDBAdapter.KEY_COMENTARIO));
 				e.setMarco(marco);
 				e.setRutaFoto(mParams.getString(EventosDBAdapter.KEY_RUTA_FOTO));
 				e.setEstado(1);
 				
 				dbaEventos = new EventosDBAdapter(EventoMarcoActivity.this);
 				dbaEventos.open();
 				if (dbaEventos.guardar(e))
 				{
 					dbaEventos.close();
 					Intent i = new Intent(EventoMarcoActivity.this, ListadoEventos.class);
 					startActivity(i);
 				}
 				
 				
 				
 				
 			}
         });

    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }

        public int getCount() {
            return Imgid.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);

            imgView.setImageResource(Imgid[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
        }


    }

}
